package com.example.administrator.officeplatform.kj;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.administrator.officeplatform.MyApplication;
import com.example.administrator.officeplatform.R;
import com.example.administrator.officeplatform.adapter.TimeSearchAdapter;
import com.example.administrator.officeplatform.adapter.WorkflowConfigVoAdapter;
import com.example.administrator.officeplatform.base.BaseActivity;
import com.example.administrator.officeplatform.bean.TimeInfo;
import com.example.administrator.officeplatform.bean.WorkflowConfigVoInfo;
import com.example.administrator.officeplatform.utils.JsonUtil;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/12/25.
 */

public class WorkflowConfigActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rv)
    RecyclerView rv;
    private Context mContext;
    private List<TimeInfo> ls_audit = new ArrayList<>();
    private WorkflowConfigVoAdapter workflowConfigVoAdapter;
    private OkHttpClient mOkHttpClient;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_time);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mContext=this;
        //设置布局管理器
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setHasFixedSize(true);
        rv.setItemAnimator(new DefaultItemAnimator());

    }

    @Override
    protected void initData() {
        toolbar.setTitle("流程配置");
        type=(String)getIntent().getExtras().get("type");
        ls_audit= new ArrayList<>();
        search();
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void search(){

        mOkHttpClient = new OkHttpClient();
        Map<String, Object> map = new HashMap<>();
        map.put("processType", type);
        String json = JSON.toJSONString(map);
        RequestBody formBody =  FormBody.create(MediaType.parse("application/json;charset=UTF-8"),json);
        String s = MyApplication.getMsession();
        Request request = new Request.Builder()
                .url(IpUtil.getIP()+"/reimbursement/workflowConfig/selectWorkflow")
                .header("cookie", MyApplication.getMsession())
                .addHeader("Content-Type","application/json;charset=UTF-8")
                .post(formBody)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(mContext, "连接异常", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                if (str.indexOf("contentList") >= 0) {
                    String contentList = JsonUtil.getMsg(str, "contentList");
                    final List<WorkflowConfigVoInfo> lsMap = JsonUtil.getPersons(contentList, new TypeToken<List<WorkflowConfigVoInfo>>() {
                    }.getType());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(lsMap.size()==0){
                                Toast.makeText(mContext, "未查询到流程", Toast.LENGTH_SHORT).show();

                            }
                            if (workflowConfigVoAdapter == null) {
                                workflowConfigVoAdapter = new WorkflowConfigVoAdapter(lsMap, mContext);
                                rv.setAdapter(workflowConfigVoAdapter);
                            } else {
                                workflowConfigVoAdapter.notifyDataSetChanged();
                            }
                            workflowConfigVoAdapter.setOnRecyclerViewListener(new WorkflowConfigVoAdapter.OnRecyclerViewListener() {
                                @Override
                                public void onItemClick(int position) {
                                    Intent mIntent = new Intent();
                                    WorkflowConfigVoInfo workflowConfigVoInfo = lsMap.get(position);
                                    //把返回数据存入Intent
                                    mIntent.putExtra("processName", workflowConfigVoInfo.getProcessName()+"  —  "+workflowConfigVoInfo.getDepartmentName());
                                    mIntent.putExtra("processId", workflowConfigVoInfo.getId());
                                    //设置返回数据
                                    setResult(7, mIntent);
                                    finish();
                                }
                            });
                        }
                    });
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mContext, "获取流程失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });


    }
}
